// The Vue build version to load with the `import` command
import Vue from 'vue'
import App from './App'
import './assets/theme/index.css'
// import VueRouter from 'vue-router'
import { routeMap } from './router-config'
// import NotFound from './page/error-page/404.vue'

import router from './router/router-components'
import ElementUI from 'element-ui'
import jquery from 'jquery'
import echarts from 'echarts'
import 'element-ui/lib/theme-chalk/index.css'
import Header from './components/header.vue'
import VueClipboard from 'vue-clipboard2' // 引入


Vue.component('Header', Header)
    // ajax封装start---------
import axios from 'axios'
import { post, toast } from './lib/js/http.js'
import locache from './lib/js/locache.js'
//管道过滤器有需要 可在其中添加(全局以及注册不需要单独引入↓↓↓↓↓↓↓↓↓);
import * as filters from './lib/js/filters';
// 全局导入过滤器
Object.keys(filters).forEach(key => Vue.filter(key, filters[key]));

Vue.prototype.$post = post; //全局使用
Vue.prototype.$toast = toast; //全局使用
// ajax封装end-----------
Vue.use(ElementUI)
Vue.use(VueClipboard) // 全局使用插件
    // Vue.use(router)
    //模拟菜单权限数据;
    // import * as menudata from './menudata.js';
const NotFound = () =>
    import ('./page/error-page/404.vue');
//授权数据处理
const formatRoutes = function(routes, routeData) {
    routes.length && routes.forEach(route => {
        if (!routeData) {
            let custom = {
                path: route.path,
                name: route.index,
                // 组件匹配成功的话才可以访问具体的页面
                component: routeMap[route.component],
                children: [],
            }
            routeData = custom;
        }
        if (route.component && routeData) {
            route.component = routeMap[route.component];
            let child = {
                path: route.path,
                name: route.index,
                component: route.component,
                meta: {
                    title: route.title,
                }
            }
            if (route.keepAlive) {
                child.meta.keepAlive = route.keepAlive;
            }
            routeData.children.push(child)
        }
        if (route.children && route.children.length) {
            formatRoutes(route.children, routeData);
        }
    });

    return routeData;

};
let isFetchRemote = true;
//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
    const username = localStorage.getItem('username');
    const menudata = localStorage.getItem('menudata') ? localStorage.getItem('menudata') : '';
    if (!username && to.path !== '/') {
        if(to.path=='/register'){
            next();
        }else{
            next({ path: '/' });
        }
    } else if (isFetchRemote && to.path !== '/'&&to.path!=='/register' || (to.path == '/dataStatistics' && menudata == ''&&to.path!=='/register')) {
        post({
            url: 'Partner/Menu/partnerMenuPc',
            data: {},
            success: res => {
                if (res.code === 1000) {
                    isFetchRemote = false;
                    const menuDataold = res.data;
                    localStorage.setItem('menudata', JSON.stringify(menuDataold));
                    const routeData = formatRoutes(menuDataold);
                    router.addRoutes([routeData].concat([
                        { name: '404', path: '/error404', component: NotFound },
                        { path: '*', redirect: '/error404' }
                    ]));
                    router.push({
                        path: to.path,
                        query: to.query
                    });
                    next();
                } else {
                    isFetchRemote = true;
                    next();
                }

            }
        });
    } else {
       
        next();
    }
});

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    components: { App },
    render: h => h(App)
});